﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dllLink
{
    public class Interface
    {
        /*****************************************************************************
         *                                                                           *
         *         Constantes utiles pour les différents appels de méthodes          *
         *                                                                           *
         * **************************************************************************/
        public static String[] Naming = {"JPM STAR GARANTI",
                                            "JPMF - Europe Equity Fund",
                                            "JPMF - France Selection",
                                            "JPMF - US Select Equity Fund",
                                            "JPMF - Emerging Markets Select Equity",
                                            "JPMF - Pacific Equity bund",
                                            "JPMF - Horizon International",
                                            "JPMF - International Equity",
                                            "JPMF - Global Strategic Bund",
                                            "JPMF - US Bond Fund",
                                            "JPMF - USD Global Bond Fund",
                                            "Cash €",
                                            "Future EUR/USD"};
        public static String[] Devise ={"€",
                                            "€",
                                            "€",
                                            "$",
                                            "$",
                                            "$",
                                            "€",
                                            "€",
                                            "$",
                                            "$",
                                            "$",
                                            "€",
                                            ""};


        public static String ProduitStruc = "ProduitStruct";
        public static String EURODOL = "EURDOL";
        public static String cotation = "Cotation";
        public static String rendement = "Rendement";
        public static String date = "Date";
        public static String EURIBOR1M = "SR1";
        public static String EURIBOR3M = "SR2";
        public static String EONIA = "SR3";
        public static String FIRSTFEDFIN = "SR4";
        public static String LIBOR1M = "SR5";
        public static String LIBOR3M = "SR6";
        
        
        
        
        
        /*****************************************************************************
         *                                                                           *
         *                 Interface concernant les fonctions de la classe BD        *
         *                                                                           *
         * **************************************************************************/
        private moteurbd bd;
        
        public void connectBD()
        {
            this.bd = new moteurbd();
            this.bd.Connect();
        }
        public void disconnectBD()
        {
            if(this.bd!=null)
                this.bd.Disconnect();
        }

        public AdpDts selectBD(String select, String from, String where)
        {
            if (bd == null)
                return new AdpDts();
            return bd.Select(select, from, where);
        }

        public String dateMinCoteBD(String OPCVM)
        {
            if (bd == null)
                return null;
            return bd.dateMinCote(OPCVM);
        }

        public String dateMaxCoteBD(String OPCVM)
        {
            if (bd == null)
                return null;
            return bd.dateMaxCote(OPCVM);
        }

        public Double firstCoteBD(String OPCVM)
        {
            if (bd == null)
                return -1;
            return bd.FirstCote(OPCVM);
        }

        public Double lastCoteBD(String OPCVM)
        {
            if (bd == null)
                return -1;
            return bd.LastCote(OPCVM);
        }


        /*****************************************************************************
         *                                                                           *
         *           Interface concernant les fonctions de la classe Import          *
         *                                                                           *
         * **************************************************************************/

        public void importationFromExcelpublic(string[,] OPCVM1, string[,] OPCVM2, string[,] OPCVM3, string[,] OPCVM4, string[,] OPCVM5, string[,] OPCVM6, string[,] OPCVM7, string[,] OPCVM8, string[,] OPCVM9, string[,] OPCVM10, string[,] EURDOL, string[,] produitStruct,string[,] SR1, string[,] SR2, string[,] SR3, string[,] SR4, string[,] SR5, string[,] SR6)
        {
            Import importer = new Import();
            importer.ImportInitial(OPCVM1, OPCVM2, OPCVM3, OPCVM4, OPCVM5, OPCVM6, OPCVM7, OPCVM8, OPCVM9, OPCVM10, EURDOL, produitStruct,SR1,SR2,SR3,SR4,SR5,SR6);
        }

        public void updateFromExcelpublic(string[,] OPCVM1, string[,] OPCVM2, string[,] OPCVM3, string[,] OPCVM4, string[,] OPCVM5, string[,] OPCVM6, string[,] OPCVM7, string[,] OPCVM8, string[,] OPCVM9, string[,] OPCVM10, string[,] EURDOL, string[,] produitStruct, string[,] SR1, string[,] SR2, string[,] SR3, string[,] SR4, string[,] SR5, string[,] SR6)
        {
            Import importer = new Import();
            importer.Update(OPCVM1, OPCVM2, OPCVM3, OPCVM4, OPCVM5, OPCVM6, OPCVM7, OPCVM8, OPCVM9, OPCVM10, EURDOL, produitStruct, SR1, SR2, SR3, SR4, SR5, SR6);
        }

        /*****************************************************************************
         *                                                                           *
         *         Interface concernant les fonctions de pricing du produit          *
         *                                                                           *
         * **************************************************************************/

        public double[,] computePrice(ulong N)
        {
            int myNbUnderlyings = 10;
            int myNbAssets = myNbUnderlyings + 3;
            string[] myAssetNames = new string[myNbAssets];
            string[] myAssetTypes = new string[myNbAssets];
            string[] myAssetCurrencies = new string[myNbAssets];
            for (int i = 1; i <= myNbUnderlyings; i++)
            {
                myAssetNames[i - 1] = "OPCVM" + i;
                myAssetTypes[i - 1] = "OPCVM";
                myAssetCurrencies[i - 1] = Devise[i];
            }
            myAssetNames[myNbUnderlyings] = EURODOL;
            myAssetTypes[myNbUnderlyings] = "EXCHANGERATE";
            myAssetCurrencies[myNbUnderlyings] = "";
            myAssetNames[myNbUnderlyings + 1] = EURIBOR1M; // taux court EUR
            myAssetTypes[myNbUnderlyings + 1] = "INTERESTRATE";
            myAssetCurrencies[myNbUnderlyings + 1] = "€";
            myAssetNames[myNbUnderlyings + 2] = LIBOR1M; // taux court USD
            myAssetTypes[myNbUnderlyings + 2] = "INTERESTRATE";
            myAssetCurrencies[myNbUnderlyings + 2] = "$";

            int myNbFixingDates = 11;
            DateTime[] myFixingDates = new DateTime[myNbFixingDates];
            myFixingDates[0] = new DateTime(2002, 08, 07);
            myFixingDates[1] = new DateTime(2003, 08, 07);
            myFixingDates[2] = new DateTime(2004, 08, 09);
            myFixingDates[3] = new DateTime(2005, 08, 08);
            myFixingDates[4] = new DateTime(2006, 08, 07);
            myFixingDates[5] = new DateTime(2007, 08, 07);
            myFixingDates[6] = new DateTime(2008, 08, 07);
            myFixingDates[7] = new DateTime(2009, 08, 07);
            myFixingDates[8] = new DateTime(2010, 08, 09);
            myFixingDates[9] = new DateTime(2011, 08, 08);
            myFixingDates[10] = new DateTime(2012, 07, 30);
            Product myProduct = new Product(myAssetNames, myAssetTypes, myAssetCurrencies, myFixingDates);

            DateTime myPricingDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
            TimeSpan myHistoryDepth = new TimeSpan(360, 0, 0, 0);

            Pricer myPricer = new Pricer(myProduct);
            TimeSpan myRebalancingPeriod = new TimeSpan(7, 0, 0, 0); //non utilisé (en l'absence de coûts de transactions)
            ulong myNbDraws = N;
            GSLFunctions.initSeed();
            return myPricer.mPrice(myPricingDate, myRebalancingPeriod, myNbDraws, myHistoryDepth);
        }


        public static void testCalibTaux()
        {
            string myIRName = EURIBOR3M; // taux court EUR
            
            DateTime myPricingDate = new DateTime(2009, 02, 25);
            TimeSpan myHistoryDepth = new TimeSpan(360, 0, 0, 0);

            //VasicekEmpiricalEstimator myVEE = new VasicekEmpiricalEstimator(myIRName, myPricingDate, myHistoryDepth);
            VasicekEmpiricalEstimator myVEE = new VasicekEmpiricalEstimator(myIRName, myPricingDate, myHistoryDepth);
            double[] myParam = myVEE.mComputeParams();
            //
            Console.WriteLine("EUR");
            Console.WriteLine("a=" + myParam[0]);
            Console.WriteLine("b=" + myParam[1]);
            Console.WriteLine("sigma=" + myParam[2]);
            
            myIRName = LIBOR1M; // taux court USD
            myVEE = new VasicekEmpiricalEstimator(myIRName, myPricingDate, myHistoryDepth);
            myParam = myVEE.mComputeParams();
            //
            Console.WriteLine("USD");
            Console.WriteLine("a=" + myParam[0]);
            Console.WriteLine("b=" + myParam[1]);
            Console.WriteLine("sigma=" + myParam[2]);
        }


        public static void test5()
        {
            int myNbUnderlyings = 10;
            int myNbAssets = myNbUnderlyings + 3;
            string[] myAssetNames = new string[myNbAssets];
            string[] myAssetTypes = new string[myNbAssets];
            string[] myAssetCurrencies = new string[myNbAssets];
            for (int i = 1; i <= myNbUnderlyings; i++)
            {
                myAssetNames[i - 1] = "OPCVM" + i;
                myAssetTypes[i - 1] = "OPCVM";
                myAssetCurrencies[i - 1] = Devise[i];
            }
            myAssetNames[myNbUnderlyings] = EURODOL;
            myAssetTypes[myNbUnderlyings] = "EXCHANGERATE";
            myAssetCurrencies[myNbUnderlyings] = "";
            myAssetNames[myNbUnderlyings + 1] = EURIBOR1M; // taux court EUR
            myAssetTypes[myNbUnderlyings + 1] = "INTERESTRATE";
            myAssetCurrencies[myNbUnderlyings + 1] = "€";
            myAssetNames[myNbUnderlyings + 2] = LIBOR1M; // taux court USD
            myAssetTypes[myNbUnderlyings + 2] = "INTERESTRATE";
            myAssetCurrencies[myNbUnderlyings + 2] = "$";

            int myNbFixingDates = 11;
            DateTime[] myFixingDates = new DateTime[myNbFixingDates];
            myFixingDates[0] = new DateTime(2002, 08, 07);
            myFixingDates[1] = new DateTime(2003, 08, 07);
            myFixingDates[2] = new DateTime(2004, 08, 09);
            myFixingDates[3] = new DateTime(2005, 08, 08);
            myFixingDates[4] = new DateTime(2006, 08, 07);
            myFixingDates[5] = new DateTime(2007, 08, 07);
            myFixingDates[6] = new DateTime(2008, 08, 07);
            myFixingDates[7] = new DateTime(2009, 08, 07);
            myFixingDates[8] = new DateTime(2010, 08, 09);
            myFixingDates[9] = new DateTime(2011, 08, 08);
            myFixingDates[10] = new DateTime(2012, 07, 30);
            Product myProduct = new Product(myAssetNames, myAssetTypes,myAssetCurrencies, myFixingDates);

            DateTime myPricingDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
            TimeSpan myHistoryDepth = new TimeSpan(360, 0, 0, 0);

            Pricer myPricer = new Pricer(myProduct);
            TimeSpan myRebalancingPeriod = new TimeSpan(7, 0, 0, 0);
            ulong myNbDraws = 1000;
            GSLFunctions.initSeed();
            Double[,] Price = myPricer.mPrice(myPricingDate, myRebalancingPeriod, myNbDraws, myHistoryDepth);
            for (int j = 0; j < myNbUnderlyings + 3; j++ )
            {
                Console.WriteLine(Price[j, 0] + " " + Price[j, 1] + " " + Price[j, 2]);
            }
        }


    }
}
